package com.shiyi.sqlSession;

import com.shiyi.pojo.Configration;
import com.shiyi.pojo.MappedStatement;

import java.beans.IntrospectionException;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

/**
 * @Descrption
 * @Author: 十一
 * @Date: 2020/11/9 14:51
 * @Version V5.0
 **/
public class DefaultSqlSession implements SqlSession {

    @Override
    public <T> T insert(String statementId, Object... params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
        MappedStatement mappedStatement = configration.getSqlMapStatementMap().get(statementId);
        SimpleExcutor excutor = new SimpleExcutor();
        return excutor.insert(configration, mappedStatement, params);
    }

    @Override
    public <T> T update(String statementId, Object... params) throws ClassNotFoundException, SQLException, NoSuchFieldException, IllegalAccessException {
        MappedStatement mappedStatement = configration.getSqlMapStatementMap().get(statementId);
        SimpleExcutor excutor = new SimpleExcutor();
        return excutor.update(configration, mappedStatement, params);
    }

    @Override
    public <T> T delete(String statementId, Object... params) throws ClassNotFoundException, SQLException, NoSuchFieldException, IllegalAccessException {
        MappedStatement mappedStatement = configration.getSqlMapStatementMap().get(statementId);
        SimpleExcutor excutor = new SimpleExcutor();
        return excutor.delete(configration, mappedStatement, params);
    }

    private Configration configration;

    public DefaultSqlSession(Configration configration) {
        this.configration = configration;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws SQLException, IllegalAccessException, IntrospectionException, InstantiationException, ClassNotFoundException, InvocationTargetException, NoSuchFieldException {
        List<Object> resultList = selectList(statementId, params);
        if(resultList.size() == 1){
            return (T)resultList.get(0);
        }else{
            throw new RuntimeException("selectOne expect 1 record,but found " + resultList.size());
        }
    }

    /**
     *功能描述 代理方法，根据方法的返回结果判断调用selectList还是selectOne
     * @author 十一
     * @date 2020/11/9 15:13
     */
    @Override
    public <T> T getMapper(Class<T> clazz) {
        // 生成动态代理的代理对象
        Object proxyClass = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{clazz}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                String className = method.getDeclaringClass().getName();
                String methodName = method.getName();
                String statementId = className + "." + methodName;
                Type returnType = method.getGenericReturnType();
                if(methodName.startsWith("select")){
                    // 判断返回值类型是否是泛型化处理
                    if(returnType instanceof ParameterizedType){
                        return selectList(statementId, args);
                    }
                    return selectOne(statementId,args);
                }else if(methodName.startsWith("insert")){
                    return insert(statementId, args);
                }else if(methodName.startsWith("update")){
                    return update(statementId, args);
                }else if(methodName.startsWith("delete")){
                    return delete(statementId, args);
                }
                throw new RuntimeException("位置的异常");
            }

        });
        return (T) proxyClass;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws SQLException, IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, InvocationTargetException, ClassNotFoundException {
        SimpleExcutor excutor = new SimpleExcutor();
        List<Object> resultList = excutor.query(configration, configration.getSqlMapStatementMap().get(statementId), params);
        return (List<E>) resultList;
    }
}
